package com.ruoyi.stuclient.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.stuclient.domain.EwcChapter;
import com.ruoyi.stuclient.domain.dto.ChapterRaiseHandDto;
import com.ruoyi.stuclient.domain.vo.EwcCourseVo;
import com.ruoyi.stuclient.service.IEwcChapterService;
import com.ruoyi.stuclient.service.IEwcCourseService;
import com.ruoyi.stuclient.service.IEwcRaiseHandService;
import com.ruoyi.system.api.RemoteClassaiService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * 课程信息Controller
 * 
 * @author lcj
 * @date 2025-02-28
 */
@Validated
@RestController
@RequestMapping("/api/studentClient")
public class EwcCourseController extends BaseController
{
    @Autowired
    private IEwcCourseService ewcCourseService;

    @Autowired
    private IEwcChapterService iEwcChapterService;

    @Autowired
    private RemoteClassaiService remoteClassaiService;

    @Autowired
    private IEwcRaiseHandService iEwcRaiseHandService;

    /**
     * 查询课程信息列表
     */
    @Operation(summary = "学生端我的页面查询课程信息列表")
    @GetMapping("/getCourseListInfo")
    public AjaxResult getCourseListInfo()
    {
        List<EwcCourseVo> listCourse = ewcCourseService.getCourseListInfo(ThirdSessionHolder.getThirdSession().getId());
        JSONObject resultJson = new JSONObject();
        resultJson.put("listCourse", listCourse);
        return AjaxResult.success("查询成功", resultJson);
    }

    /**
     * 举手
     */
    @Operation(summary = "学生端课程页面举手")
    @PostMapping("/raiseHand")
    public AjaxResult raiseHand(@Valid @RequestBody ChapterRaiseHandDto chapterRaiseHandDto)
    {
        //查询课程状态
        EwcChapter chapter = iEwcChapterService.getChapterStatus(chapterRaiseHandDto);
        if(Objects.nonNull(chapter) && chapter.getStatus() == 4 ){//授课中
            //举手
            AjaxResult result = remoteClassaiService.getStudentHand(ThirdSessionHolder.getThirdSession().getId(),chapterRaiseHandDto.getTeacherId(), SecurityConstants.INNER);
            //记录举手信息
            if(result.isSuccess()){
                iEwcRaiseHandService.saveRaiseRecord(chapterRaiseHandDto);
            }
            return result;
        }
        return AjaxResult.error("课程未在授课中");
    }

}
